package cn.xyf.mzmy.server.mq.consumer;


import cn.xyf.mzmy.server.mq.message.DemoMessage;
import com.alibaba.fastjson2.JSON;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DemoConsumer {

    @Autowired
    private RedissonClient redissonClient;

    @PostConstruct
    public void listenDemoMessage(){
        // 获取主题（消息队列）
        RTopic topic = redissonClient.getTopic("demoQueue");

        topic.addListener(DemoMessage.class, (channel, message) -> {
            System.out.println("收到消息：" + JSON.toJSONString(message));
        });
    }

    @PostConstruct
    public void listenDelayDemoMessage(){
        // 获取底层阻塞队列
        RBlockingDeque<DemoMessage> blockingDeque = redissonClient.getBlockingDeque("delayQueue");
        new Thread(()-> {
            try {
                while (true){
                    DemoMessage demoMessage = blockingDeque.take();
                    System.out.println("收到消息：" + JSON.toJSONString(demoMessage));
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }


}
